package softtech.apps.customadapter;

import java.util.List;

import softtech.apps.autoanswerphonecall.R;
import softtech.apps.model.DatabaseHandler;
import softtech.apps.model.Timer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemTimingAdapter extends BaseAdapter{

	private Context mContext;
	
	private List<Timer> listTimer;
	
	DatabaseHandler dbHandler;
	
	public ItemTimingAdapter(Context context, List<Timer> arrTime){
		mContext = context;
		
		dbHandler = new DatabaseHandler(mContext);
		
		listTimer = arrTime;
	}
	
	public void updateListTimer(List<Timer> arrTime){
		
		listTimer = arrTime;
		
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listTimer.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return listTimer.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewHolder holder = new ViewHolder();
		
		if(convertView == null){

			convertView = inflater.inflate(R.layout.item_timing, null);

			holder.imgRepeat = (ImageView) convertView.findViewById(R.id.imgRepeat);
			
			holder.cbActivity = (CheckBox) convertView.findViewById(R.id.cbActive); 
			
			holder.tvName = (TextView) convertView.findViewById(R.id.tvNameItem);
			
			holder.tvDate = (TextView) convertView.findViewById(R.id.tvDateItem);
			
			convertView.setTag(holder);
			
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(listTimer.get(position).getRepeat() == 1){
			holder.imgRepeat.setVisibility(View.VISIBLE);
		}else{
			holder.imgRepeat.setVisibility(View.GONE);
		}
		
		holder.tvName.setText(listTimer.get(position).getNameTask());
		
		holder.tvDate.setText(listTimer.get(position).getDates());
		
		final int pos = position;
		
		if(listTimer.get(position).getActive() >= 1){
			holder.cbActivity.setChecked(true);
		}else{
			holder.cbActivity.setChecked(false);
		}
		
		holder.cbActivity.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton view, boolean isChecked) {
				// TODO Auto-generated method stub
				// update database
				if(isChecked){
					String dates = listTimer.get(pos).getDates();

					int count = 0;

					if (dates.contains("Mon")) {
						count += 1;
					}

					if (dates.contains("Tue")) {
						count += 1;
					}

					if (dates.contains("Wed")) {
						count += 1;
					}

					if (dates.contains("Thu")) {
						count += 1;
					}

					if (dates.contains("Fri")) {
						count += 1;
					}

					if (dates.contains("Sat")) {
						count += 1;
					}

					if (dates.contains("Sun")) {
						count += 1;
					}
					
					listTimer.get(pos).setActive(count);
					
				}else{
					listTimer.get(pos).setActive(0);
				}
				
				if(dbHandler == null){
					dbHandler = new DatabaseHandler(mContext) ;
				}
				
				dbHandler.updateTimer(listTimer.get(pos));
				
				notifyDataSetChanged();
				//update Time of List Task
			}
		});
		
		return convertView;
	}

	static class ViewHolder {

		CheckBox cbActivity;
		ImageView imgRepeat;
		TextView tvName;
		TextView tvDate;

	}
	
}
